﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Member;
using WuDada.Core.Member.Service;
using WuDada.Core.Member.Domain;
using WuDada.Core.Post.Domain;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;

public partial class joinmember : System.Web.UI.Page
{
    private MemberFactory m_MemberFactory;
    private IMemberService m_MemberService;
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private HttpHelper m_HttpHelper;
    private WebMailService m_WebMailService;
    
    //縣市地區分類 NodeId=43
    private int m_NodeId = 43;


    protected void Page_Load(object sender, EventArgs e)
    {
        m_MemberFactory = new MemberFactory();
        m_MemberService = m_MemberFactory.GetMemberService();
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_HttpHelper = new HttpHelper();
        m_WebMailService = new WebMailService();

        if (!IsPostBack)
        {
            initCity();
        }
    }

    private void initCity()
    {
        ddlCity.Items.Clear();
        ddlCountry.Items.Clear();
        IList<NodeVO> subNodeList = m_PostService.GetNodeListByParentId(m_NodeId);
        ddlCity.Items.Add(new ListItem("請選擇縣市", ""));
        ddlCountry.Items.Add(new ListItem("請選擇鄉鎮市區", ""));
        if (subNodeList != null && subNodeList.Count > 0)
        {
            foreach (NodeVO nodeVO in subNodeList)
            {
                ddlCity.Items.Add(new ListItem(nodeVO.Name, nodeVO.NodeId.ToString()));
            }
        }
    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        string city = ddlCity.SelectedValue;
        if (string.IsNullOrEmpty(city))
        {
            ddlCountry.Items.Clear();
            ddlCountry.Items.Add(new ListItem("請選擇鄉鎮市區", ""));
        }
        else
        {
            ddlCountry.Items.Clear();
            ddlCountry.Items.Add(new ListItem("請選擇鄉鎮市區", ""));
            IList<NodeVO> subNodeList = m_PostService.GetNodeListByParentId(int.Parse(city));            
            if (subNodeList != null && subNodeList.Count > 0)
            {
                foreach (NodeVO nodeVO in subNodeList)
                {
                    ddlCountry.Items.Add(new ListItem(nodeVO.Name, nodeVO.HtmlContent));
                }
            }
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        MemberVO memberVO = new MemberVO();
        UIHelper.FillVO(PanelUI, memberVO);
        if (m_MemberService.GetMemberByLoginId(memberVO.LoginId) != null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("此帳號已經註冊過了，請輸入其他帳號"), false);
        }
        else
        {
            List<string> professional = new List<string>();
            foreach (ListItem li in ckblProfessional.Items)
            {
                if (li.Selected)
                {
                    professional.Add(li.Value);
                }
            }
            if (ckbProfessionalOther.Checked || !string.IsNullOrEmpty(txtProfessionalOther.Text))
            {
                professional.Add(ckbProfessionalOther.Text);
            }
            memberVO.Professional = string.Join(";", professional.ToArray());
            if (!string.IsNullOrEmpty(ddlCity.SelectedValue))
            {
                memberVO.CompanyAddressCity = ddlCity.SelectedItem.Text;
            }
            if (!string.IsNullOrEmpty(ddlCountry.SelectedValue))
            {
                memberVO.CompanyAddressCountry = ddlCountry.SelectedItem.Text;
                memberVO.CompanyAddressZip = ddlCountry.SelectedValue;
            }
            memberVO.Email = memberVO.LoginId;
            memberVO.Token = Guid.NewGuid().ToString();
            memberVO.CreatedDate = DateTime.Now;
            memberVO.UpdatedDate = DateTime.Now;
            memberVO.CreateIP = m_HttpHelper.GetUserIp(Context);
            memberVO.Status = "0";
            memberVO = m_MemberService.CreateMember(memberVO);            

            //發信
            //m_WebMailService.SendConfirmMail_ToMember(memberVO);
            m_WebMailService.SendConfirmMail_ToContact(memberVO);
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("您的會員資料已送出審核，請您耐心等候，謝謝您。", "login.aspx"), false);
        }
    }

  
}